﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Cherry.Agile.RpcService.ExtProcessor;
using System.Data;

namespace Cherry.Agile.RpcService.Extentor
{
    /// <summary>
    /// PRC配置跟踪输出页面抽像类
    /// Create laiguoxin@szxys.cn
    /// </summary>
    public abstract class TracerPage: System.Web.UI.Page
    {

        protected string ItemStings = @",-选择-;
Error,最近的请求异常信息;
TraceMessage,最近的请求跟踪信息;
ConfigLoadTrace,显示配置加载跟踪;
Processor,显示已加载的业务插件;
Assembly,显示已加载的插件程序集;";

        /// <summary>
        /// 由实现页面提供一个GridView控件显示数据
        /// </summary>
        protected abstract GridView DataView { get; }

        /// <summary>
        /// 由实现页面提供一个DropDownList控件显示选择项
        /// </summary>
        protected abstract DropDownList SelectList { get; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            SelectList.SelectedIndexChanged += (s, args) =>
                {
                    BindingData(SelectList.SelectedItem.Value);
                };
            config = ServiceConfigManager.ServiceConfig;
            tracer = config.ExtentProcessor.GetExtentor<TopTenTracer>();
            if (!IsPostBack)
            {
                string[] items = ItemStings.Split(new char[]{';'}, StringSplitOptions.RemoveEmptyEntries);
                SelectList.Items.Clear();
                SelectList.AutoPostBack = true;
                foreach (string item in items)
                {
                    string[] s = item.Trim().Split(new char[] { ',' });
                    SelectList.Items.Add(new ListItem(s[1],s[0]));
                }
            }
        }


        protected virtual void BindingData(string item)
        {
            object data = GetItemData(item);
            if (data is string) data = new string[] { (string)data};
            DataView.DataSource = data;
            DataView.DataBind();
        }

        private ServiceConfigInfo config;
        private TopTenTracer tracer;

        private object GetItemData(string item)
        {
            if (!config.TraceEnabled)
            {
                return "RpcService通信服务跟踪功能已关闭。";
            }
            switch (item)
            {
                case "Assembly":
                    {
                        return config.AssemblyList.Values;
                    }
                case "Processor":
                    {
                        return config.ProcessModuleList.Values.OrderBy(m =>m.RequestCode);
                    }
                case "ConfigLoadTrace":
                    {
                        return  config.LoadTraceMessage.Replace("\n", "\n<br />") ;
                    }
                case "TraceMessage":
                    {
                        if (tracer == null)
                            return  "Tracer 没有启用";
                        else
                            return tracer.LatestTrace;
                    }
                case "Error":
                    {
                        if (tracer == null)
                            return "Tracer 没有启用";
                        else
                            return tracer.LatestError;
                    }
                default:
                    {
                        return "请选择" ;
                    }
            }
        }
    }
}
